package TranstracDemo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import com.situ.student.entity.Student;
import com.situ.student.util.JDBCUtil;

public class TranstracDemo {
	private Object callableStatement;

	public void init() {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		String sql1 = "update count1 set money=money-1000 where name='张三';";
		String sql2 = "update count1 set money=money+1000 where name='李四';";
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			preparedStatement = connection.prepareStatement(sql1);
			preparedStatement = connection.prepareStatement(sql2);
			preparedStatement.executeUpdate();

			connection.commit();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public void test1() {
		Connection connection = null;
		callableStatement = null;
		ResultSet resultSet = null;
	
		try {
			connection = JDBCUtil.getConnection();
			callableStatement = connection.prepareCall("call student");
			resultSet = ((PreparedStatement) callableStatement).executeQuery();
			while(resultSet.next()){
				int id =resultSet.getInt("id");
				String  name =resultSet.getString("name");
				Integer age =resultSet.getInt("age");
				String  gender =resultSet.getString("gender");
				Student student =new Student(id,name ,age,gender);
				System.out.println(student);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, null, resultSet);
		}

	}

}
